<?php

namespace App\AdminModule\Presenters;

use \Nette\Application\UI\Form,
    App\Model\Dao\Shortnew,
     \Nette\Utils\Image,
    \Nette\Utils\Strings,
    \Grido, 
    \Grido\Grid;

/**
 * Short news presenter.
 */
class ShortnewPresenter extends BasePresenter {
 
  /** @var \Kdyby\Doctrine\EntityManager @inject */
  public $em;
  
  private $shortnew = NULL;
  
  /**
   * Add action
   * @param int $id
   * @return boolean
   */
  public function actionAdd($id){
    if(!isset($id)){
      return;
    }
    
    $this->shortnew = $this->em->getDao(Shortnew::getClassName())->find($id);
  }
  
  /**
   * Render add
   * @param int $id
   */
  public function renderAdd($id){
    $this->template->shortnew = $this->shortnew;
  }
  
  /**
   * Delete short new
   * @param int $id
   */
  public function handleDelete($id){
    $shortnew = $this->em->getDao(Shortnew::getClassName())->find($id);
    @unlink(ROOT_DIR . '/content/shortnew/' . $shortnew->getPhoto() . '/image.jpg');
    @rmdir(ROOT_DIR . '/content/shortnew/' . $shortnew->getPhoto());
    
    if($shortnew){
      $this->em->remove($shortnew);
      $this->em->flush();
    }
    
    $this->redirect('Shortnew:default');
  }
  
  /**
   * Short new form
   * @param String $name
   * @return \Nette\Application\UI\Form
   */
  public function createComponentAddShortnewForm($name){
    $form = new Form();
    $form->addText('title', 'Titulek')->addRule(Form::FILLED, 'Titulek musí být vyplněn');
    $form->addTextArea('content', 'Obsah')->addRule(Form::FILLED, 'Titulek musí být vyplněn');
    $form->addUpload('photo', 'Fotka');
    $form->addText('link', 'Link');

    if($this->shortnew){
      $form['title']->setDefaultValue($this->shortnew->getTitle());
      $form['content']->setDefaultValue($this->shortnew->getContent());
      $form['link']->setDefaultValue($this->shortnew->getLink());
    }
    
    $form->addSubmit('send');
    $form->onSuccess[] = callback($this, 'addShortnewFormSuccess');
    
    return $form;
  }
  
  /**
   * Short new form executing
   * @param type $form
   */
  public function addShortnewFormSuccess($form){
    $values = $form->getValues();

    if(isset($this->shortnew)){
      $this->shortnew->setTitle($values['title']);
      $this->shortnew->setLink($values['link']);
      $this->shortnew->setContent($values['content']);
      
      if ($values['photo']->name != "" && $values['photo']->isImage()) {
        $hash = $this->savePicture($values['photo']);
        $this->shortnew->setPhoto($hash);
      }
      
      $this->em->persist($this->shortnew);
    }else{
      $shortnew = new Shortnew();
      $shortnew->setTitle($values['title']);
      $shortnew->setLink($values['link']);
      $shortnew->setContent($values['content']);
      
      if ($values['photo']->name != "" && $values['photo']->isImage()) {
        $hash = $this->savePicture($values['photo']);
        $shortnew->setPhoto($hash);
      }
      
      $this->em->persist($shortnew);
    }

    $this->em->flush();

    $this->flashMessage('Novinka byla uložena', 'success');
    $this->redirect('Shortnew:default');
  }
  
  /**
   * Shortnew grid
   * @param String $name
   * @return \Grido\Grid
   */
  public function createComponentShortnewGrid($name){
    $qb = $this->em->createQueryBuilder()
      ->addSelect('s.id AS id')
      ->addSelect('s.title AS title')
      ->from(Shortnew::getClassName(), ' s');

    $shortnews =  $qb->getQuery()->getArrayResult();

    $g = new Grid($this, $name);

    // Grido data
    $g->model = $shortnews;
    
    $g->addColumnText('id', 'ID')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('title', 'Titulek')
        ->setSortable()
        ->setFilterText();
    
    // Create edit button
    $g->addActionHref('edit', 'Edit', 'Shortnew:add')
        ->setIcon('pencil')->setPrimaryKey('id');
    $g->addActionHref('delete', 'Smazat', 'delete!')
        ->setIcon('remove')->setPrimaryKey('id');
    
    // Return grido
    $g->filterRenderType = Grido\Components\Filters\Filter::RENDER_INNER;
    $g->setExport();
    return $g;
  }
  
  /**
   * Save picture
   * @param type $picture
   * @return string
   */
  private function savePicture($picture){
    $temp = $picture->getTemporaryFile();
		$image = Image::fromFile($temp);

		$hash = Strings::random(10) . time();
		$path = ROOT_DIR . '/content/shortnew/' . $hash;
		mkdir($path);
		$image->save($path . '/image.jpg');

		return $hash;
  }
}
